﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MallInteractiveMap.CMSWebClient.Common;
using MallInteractiveMap.CMSWebClient.Presenter;
using MallInteractiveMap.DTO;
using MallInteractiveMap.Language;

namespace MallInteractiveMap.CMSWebClient.BasicView
{
    public partial class Floor : BasePage
    {
        private FloorPresenter _presenter;
        protected void Page_Load(object sender, EventArgs e)
        {
            _presenter = new FloorPresenter();
            if (!IsPostBack)
            {
                PrepareView();
            }
        }

        public int Id
        {
            get { return Request.QueryString["Id"] != null ? Convert.ToInt32(Request.QueryString["Id"]) : 0; }
        }

        private void PrepareView()
        {
            List<DTOMap> mapList = new MapPresenter().GetMaps();
            Map.DataValueField = "Id";
            Map.DataTextField = "Title";
            Map.DataSource = mapList;
            Map.DataBind();
            if (Id > 0)
            {
                DTOFloor floor = _presenter.GetFloors(Id).FirstOrDefault();
                PopulateControls(floor);
            }

        }
        private void PopulateControls(DTOFloor floor)
        {
            Name.Text = floor.Name;
            FloorIndex.Text = floor.FloorIndex.ToString();
            Map.SelectedValue = floor.MapId.ToString();
            Description.Text = floor.Description;
        }
        private DTOFloor PopulateObject()
        {
            DTOFloor floor = new DTOFloor();
            floor.Name = Name.Text;
            floor.FloorIndex = Convert.ToInt32(FloorIndex.Text);
            floor.MapId = Convert.ToInt32(Map.SelectedValue);
            floor.Description = Description.Text;
            floor.LastUpdated = DateTime.Now;
            floor.IsNew = Id <= 0;
            floor.Id = Id;
            return floor;
        }

        protected void Save_Click(object sender, EventArgs e)
        {
            DTOFloor floor = PopulateObject();
            if (_presenter.SetData(floor))
            {
                Response.Redirect("~/BasicView/FloorDetail.aspx");
            }
            else
            {
                ShowMessage(LanguageLoader.GetText("FAILED_OPERATION_MESSAGE"), LanguageLoader.GetText("FAILED"));
            }
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            SetLanguageAndResource();
        }

        private void SetLanguageAndResource()
        {
            NameLable.Text = LanguageLoader.GetText("NAME");
            MapLabel.Text = LanguageLoader.GetText("MAP");
            FloorIndexLabel.Text = LanguageLoader.GetText("FLOOR_INDEX");
            DescriptionLable.Text = LanguageLoader.GetText("DESCRIPTION");
            SaveFloor.Text = LanguageLoader.GetText("SAVE");
        }
    }
}